import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {
    //自己实现数组转字符串
    public static String myToString(int[] array) {
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if(i != array.length-1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static int[] fun(){
        return new int[]{1,2,3,4,5};
    }

    public static int fun(int[] array,int x){
        for (int i = 0; i < array.length; i++) {
            if(array[i]==x){
                return i;
            }
        }
        return -1;
    }

    //自己实现二分法查找数组元素的下标
    public static int myBinarySearch(int[] array,int x){
        int left = 0;
        int right = array.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(array[mid]>x){
                right = mid-1;
            } else if (array[mid]<x) {
                left = mid+1;
            }else {
                return mid;
            }
        }
        return -1;
    }


    //冒泡排序
    public static void bubbleSort(int[] array){
        //i表示的是趟数
        for (int i = 0; i < array.length-1; i++) {
            boolean flg=false;
            //j 代表每一趟比较的次数
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]>array[j+1]){
                   int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if (flg == false){//说明这一次已经排完序了没有进行交换
                break;
            }
        }
    }



    public static void main(String[] args) {


        int[] array = {15,13,4,56,89};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));

//        Arrays.sort(array);
//        System.out.println(myBinarySearch(array, 89));
//        //Arrays.binarySearch(数组名, 数组元素)
//        System.out.println(Arrays.binarySearch(array, 89));
//
//        System.out.println(fun(array, 56));

//        int[] array={1,2,3,};
//        int[] array2=fun();
//       for(int i = 0;i<fun().length;i++){
//           System.out.print(array2[i]+" ");
//       }
//        System.out.println();
//       //数组转字符串
//      String s = Arrays.toString(array2);
//        System.out.println(s);
//        System.out.println(Arrays.toString(array));
//        System.out.println(myToString(array));

        //拷贝 数组
//        int[] array3 = {1,5,3,6};

//        int[] copy = new int[array3.length];
//        for (int i = 0; i <array3.length ; i++) {
//            copy[i] = array3[i];
//        }
//        System.out.println(Arrays.toString(copy));

//        int[] copy2  = Arrays.copyOf(array3,array3.length*2);
//        System.out.println(Arrays.toString(copy2));

        //拷贝下标[1,3)
//        int[] copy3  = Arrays.copyOfRange(array3,1,3);
//        System.out.println(Arrays.toString(copy3));
//
//        int[] copy4 = new int[array3.length];
//        System.arraycopy(array3,0,copy4,0,array3.length);//参数：原数组 原数组起始位置 目标数组 目标数组起始位置 拷贝的长度
//        System.out.println(Arrays.toString(copy4));



        //这不是拷贝 因为用的是同一个地址
        int[] array5 = {1,5,3,6};
        int[] copy1 =array;

     }
}